<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="120px"
    >
      <el-form-item label="属性名" prop="name">
        <el-input v-model="dataForm.name" placeholder="属性名"></el-input>
      </el-form-item>
      <el-form-item label="属性类型" prop="attrType">
        <el-select v-model="dataForm.attrType" placeholder="请选择" disabled>
          <el-option label="规格参数" :value="1"></el-option>
          <el-option label="销售属性" :value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="值类型" prop="valueType">
        <el-switch
          v-model="dataForm.valueType"
          active-text="允许多个值"
          inactive-text="只能单个值"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        ></el-switch>
      </el-form-item>
      <el-form-item label="可选值" prop="valueSelect">
        <el-select
          v-model="dataForm.valueSelect"
          multiple
          filterable
          allow-create
          placeholder="请输入内容"
        ></el-select>
      </el-form-item>
      <el-form-item label="所属分类" prop="categoryId">
        <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
      </el-form-item>
      <el-form-item label="属性组" prop="attrGroupId" v-if="type == 1">
        <el-select placeholder="请选择" v-model="dataForm.attrGroupId">
          <el-option
            v-for="item in groups"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="可检索" prop="searchType" v-if="type == 1">
        <el-switch
          v-model="dataForm.searchType"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="快速展示" prop="showDesc" v-if="type == 1">
        <el-switch
          v-model="dataForm.showDesc"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import AttrApi from "@/api/product/attribute";
import GroupApi from "@/api/product/group";
import CategoryCascader from "@/views/common/category-cascader";

export default {
  components: { CategoryCascader },
  data() {
    return {
      catelogPath: [],
      visible: false,
      groups: [],
      dataForm: {
        id: null,
        attrGroupId: null,
        valueType: 0,
        valueSelect: [],
        searchType: 0,
        showDesc: 0,
      },
      dataRule: {
        name: [{ required: true, message: "属性名不能为空", trigger: "blur" }],
        searchType: [
          {
            required: true,
            message: "是否需要检索不能为空",
            trigger: "blur",
          },
        ],
        icon: [
          { required: true, message: "属性图标不能为空", trigger: "blur" },
        ],
        attrType: [
          {
            required: true,
            message: "属性类型不能为空",
            trigger: "blur",
          },
        ],
        showDesc: [
          {
            required: true,
            message: "快速展示不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  props: {
    type: {
      type: Number,
      default: 1,
    },
  },
  created() {},
  watch: {
    catelogPath(path) {
      this.dataForm.categoryId = path[path.length - 1];
      if (path && path.length > 0) {
        GroupApi.getGroupList({ categoryId: this.dataForm.categoryId }).then(
          ({ data }) => {
            this.groups = data;
          }
        );
      }
    },
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.dataForm.attrType = this.type;
      this.visible = true;
      this.$nextTick(() => {
        if (id) {
          AttrApi.getAttr({ id: this.dataForm.id }).then(({ data }) => {
            this.dataForm = data;
            this.dataForm.valueSelect = data.valueSelect.split(";");
            this.catelogPath = data.categoryPath;
            this.$nextTick(() => {
              this.dataForm.attrGroupId = data.attrGroupId;
            });
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            this.dataForm.valueSelect = this.dataForm.valueSelect.join(";");
            AttrApi.updateAttr(this.dataForm).then(({ data }) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            });
          } else {
            this.dataForm.valueSelect = this.dataForm.valueSelect.join(";");
            AttrApi.createAttr(this.dataForm).then(({ data }) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            });
          }
        }
      });
    },
    dialogClose() {
      this.catelogPath = [];
      this.dataForm = {};
      this.$refs["dataForm"].resetFields();
    },
  },
};
</script>
